// call ajax function for wordpress
function loadAjax(data, type, fn , success){
    data += '&action=do_ajax';
    data += '&fn='+fn;
    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php?lang='+language,
        type: type,
        /*
        data:{
            'action':'do_ajax',
            'fn':'ajax_login',
            'count':10
        },
        */
        data:data,
        dataType: 'JSON',
        success:success,

        error: function(errorThrown){
            console.log(errorThrown);
        }

    });
}

//login function
function ajaxLogin($form){
    var $btn = $form.find('[type="submit"]');
    
    var data = $form.serialize();

    var result = $form.parsley( 'validate' );

    if(result){
        showLoading($btn);
        loadAjax(data,'POST', 'ajax_login' ,function(resp){
            if(resp.sts == 'success'){
                window.location = resp.url;
            }else{
                showMessage(resp.sts, resp.message, $form, $('.after'));
            }
            hideLoading($btn);
        });
    }

    return false;
}

//register function
function ajaxRegister($form){
    
    var $btn = $form.find('input[type="submit"]');
    var result = $form.parsley('validate');
    
    if(result){

        var data = $form.serialize();
        showLoading($btn);
        loadAjax(data,'POST', 'ajax_register' ,function(resp){
            if(resp.sts == 'success'){
                showMessage(resp.sts, resp.message, $form, $btn);
                window.location = resp.redirect;
            }else{
                showMessage(resp.sts, resp.message, $form, $btn);
            }
            $form.find('#change-image').click();
            hideLoading($btn);
        });
    }

    return false;
}

function showLoading($obj){
    $obj.attr('org',$obj.val());
    $obj.attr('disabled',true);
    if(language=='fr')
        $obj.val('Chargement...');
    else
        $obj.val('Loading...');      
}

function hideLoading($obj){
    $obj.val($obj.attr('org'));
    $obj.attr('org','');
    $obj.removeAttr('disabled');
}

function showMessage(type, message, $form, $after ){
    var message_html = '<div class="message-'+ type +'">'+ message +'</div>';
    $form.find('.message-error, .message-success').remove();
    if($after){
        $after.after(message_html);
    }else{
        $form.append(message_html);
    }
}

function showRecaptcha(element) {
    Recaptcha.create("6LfPgesSAAAAAOYoz5oTsMm5rbP7LhgJOQsIGTGl", element, {
      theme: "red",
      callback: captchaCallback});
}
function captchaCallback(){
    var $field =  $('#recaptcha_response_field');
    $field.attr('parsley-required','true');
    $field.attr('parsley-error-container','#recaptcha_div');
    $field.removeAttr('placeholder');
    
}

// sign in
$('#form-signin').submit(function(){
    var $this = $(this);
    var result = $this.parsley( 'validate' );
    if(result){
        ajaxLogin($this);
    }
    return false;
});

// register
$('#form-register').submit(function(){ 
    return ajaxRegister($(this));
});

//show captcha on register page
jQuery(document).ready(function(){
    if($('#recaptcha_div').length > 0){
        showRecaptcha('recaptcha_div');
    }
});